package com.tianji.learning.job;

import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.enums.LessonStatus;
import com.tianji.learning.service.ILearningLessonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author liuchun
 * @version 1.0
 * @date 2024/7/10 9:18
 */
@Slf4j
@Component
public class CheckLessonStatusJob {

    @Autowired
    private ILearningLessonService lessonService;

    @Scheduled(cron = "0 * * * * ?")
    public void LessonStatusJob() {
        //查询状态为未过期的课程，不区分用户
        List<LearningLesson> lessonList = lessonService.lambdaQuery()
                .ne(LearningLesson::getStatus, LessonStatus.EXPIRED)
                .list();
        if (CollUtils.isEmpty(lessonList)){
            return ;
        }
        //判断是否过期
        for (LearningLesson lesson : lessonList) {
            //过期时间在当前时间前面，则过期
            if (lesson.getExpireTime().isBefore(LocalDateTime.now())){
                lesson.setStatus(LessonStatus.EXPIRED);
            }
        }
        //批量更新
        lessonService.updateBatchById(lessonList);
    }
}
